import 'package:flutter/material.dart';
import 'package:flutter_demo/common_widget/my_appbar/my_app_bar.dart';

class ListVIewDemo extends StatefulWidget {
  ListVIewDemo({Key key}) : super(key: key);

  @override
  _ListVIewDemoState createState() => _ListVIewDemoState();
}

class _ListVIewDemoState extends State<ListVIewDemo> {
  final ScrollController _controller = ScrollController();

  goTop() {
    _controller.animateTo(-30.0,
        duration: Duration(seconds: 1), curve: Curves.easeIn);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(title: 'listView Demo').build(context),
      floatingActionButton: FloatingActionButton(
        child: Text('返回顶部'),
        onPressed: () {
          print(_controller.offset);
          goTop();
        },
      ),
      body: RefreshIndicator(
          child: Scrollbar(
              // isAlwaysShown: true,
              child: NotificationListener(
                  onNotification: (Notification event) {
                    // print(event);
                    return false; //不拦截冒泡事件
                  },
                  child: ListWhellScroll()
                  // GridWidget()
                  // ListView.builder(
                  //     controller: _controller,
                  //     physics: AlwaysScrollableScrollPhysics(),
                  //     //separated  可以添加分割线
                  //     itemBuilder: (BuildContext context, int index) {
                  //       return Container(
                  //         child: Text(index.toString()),
                  //         color: Colors.blue,
                  //       );
                  //     },
                  //     padding: EdgeInsets.symmetric(
                  //         vertical: 50, horizontal: 20), //添加padding
                  //     itemExtent: 50, //固定每一项的高度
                  //     // separatorBuilder: (BuildContext context, int index) {
                  //     //   return Divider(
                  //     //     thickness: 10,
                  //     //   );
                  //     // },

                  //     itemCount: 50)
                  )),
          backgroundColor: Colors.black,
          onRefresh: () async {
            await Future.delayed(Duration(seconds: 4));
          }),
    );
  }
}

class GridWidget extends StatelessWidget {
  const GridWidget({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GridView.builder(
        gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
            mainAxisSpacing: 20,
            crossAxisSpacing: 10,
            maxCrossAxisExtent: 200,
            childAspectRatio: 16 / 9),
        itemBuilder: (BuildContext context, int index) {
          return Container(
            alignment: Alignment.center,
            color: Colors.blue,
            child: Text(index.toString()),
          );
        });
  }
}

class ListWhellScroll extends StatelessWidget {
  const ListWhellScroll({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return RotatedBox(
      quarterTurns: 0,
      child: ListWheelScrollView(
          magnification: 2.8,
          // offAxisFraction: 2.0,
          // diameterRatio: 3.0,
          overAndUnderCenterOpacity: 0.4,
          physics: FixedExtentScrollPhysics(),
          itemExtent: 100,
          onSelectedItemChanged: (index) {
            print(index);
          },
          children: List.generate(10, (index) {
            return Container(
              // color: Colors.blue,
              child: Text('hello world'),
            );
          })),
    );
  }
}
